<!--
 * @Author: your name
 * @Date: 2021-05-17 21:55:25
 * @LastEditTime: 2021-05-17 22:37:27
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \vue-demo\src\views\Home\Home.vue
-->
<template>
  <div>
    <div class="table">
      <button @click="isPopup = true">添加</button>
      <div class="tr">
        <span>id</span>
        <span>姓名</span>
        <span>操作</span>
      </div>
      <div class="tr" v-for="it in tableData2" :key="it.id">
        <span>{{ it.id }}</span>
        <span>{{ it.name }}</span>
        <span @click="del(it.id)">删除</span>
      </div>
    </div>
    <div class="popup" v-show="isPopup">
      <span class="close" @click="isPopup = false">X</span>
      <p>
        <label>id:</label>
        <input type="number" v-model="addData.id" />
      </p>
      <p>
        <label>name:</label>
        <input type="text" v-model="addData.name" />
      </p>
      <p>
        <button @click="onSubmit()">提交</button>
      </p>
    </div>
    <Paging
      :pageTotal="tableData.length"
      :pageIndex="pageIndex"
      @chengePageIndex="chengePageIndex"
    ></Paging>
  </div>
</template>

<script>
import Paging from "../../components/Paging";
export default {
  data() {
    return {
      pageIndex: 1,
      tableData: [],
      isPopup: false,
      addData: {
        id: "",
        name: "",
      },
      tableData: [],
    };
  },
  computed: {
    tableData2() {
      return this.tableData.slice(
        (this.pageIndex - 1) * 40,
        this.pageIndex * 40
      );
    },
  },
  watch: {
    tableData() {
      localStorage.setItem("tableData", JSON.stringify(this.tableData));
    },
  },
  methods: {
    chengePageIndex(i) {
      this.pageIndex = i;
    },
    onSubmit() {
      if (!this.addData.id) {
        return alert("id不能为空");
      } else if (!this.addData.id) {
        return alert("姓名不能为空");
      }
      let isId = false;
      this.tableData.forEach((value) => {
        if (value.id == this.addData.id) {
          isId = true;
        }
      });
      if (isId) {
        return alert("id不允许重复");
      }

      this.tableData.push(this.addData);
      this.addData = {
        id: "",
        name: "",
      };
      this.isPopup = false;
    },
    del(id) {
      this.tableData = this.tableData.filter((value) => {
          return value.id !== id;
      });
    },
  },
  created() {
    this.tableData = JSON.parse(localStorage.getItem("tableData")) || [];
  },
  components: {
    Paging,
  },
};
</script>

<style>
.table {
  width: 80%;
  margin: 40px auto 0;
}

.table button {
  padding: 4px 6px;
  background: transparent;
  border: 1px solid #e4e7ed;
  margin-bottom: 4px;
}

.table button:hover {
  background: #3eaf7c;
}

.table .tr {
  display: flex;
  justify-content: space-evenly;
  border-bottom: 1px solid #e4e7ed;
}

.table .tr:nth-child(2n) {
  background: #ebeef5;
}

.table .tr:nth-child(2n -1) {
  background: #f2f6fc;
}

.table .tr span {
  padding: 10px 20px;
  flex: 1;
}

.table .tr span:last-child {
  cursor: pointer;
  user-select: none;
}

.table .tr:hover span:last-child {
  color: #3eaf7c;
}

.popup {
  position: fixed;
  width: 690px;
  height: 300px;
  top: 50%;
  left: 50%;
  box-shadow: 0px 0px 4px 0 #ccc;
  transform: translate(-50%, -50%);
  background: #f2f6fc;
  border-radius: 2%;
  padding-top: 60px;
}

.close {
  position: absolute;
  right: 20px;
  top: 20px;
  cursor: pointer;
  user-select: none;
  color: #3eaf7c;
}
.popup p {
  display: flex;
  align-items: center;
}

.popup p label {
  width: 80px;
  text-align: center;
}

.popup p input {
  height: 40px;
  width: 80%;
  margin: 0 auto;
  border: 1px solid #e4e7ed;
  border-radius: 10px;
  outline: none;
  border: none;
  margin: 10px 0;
  color: #3eaf7c;
}

.popup button {
  padding: 4px 6px;
  background: #3eaf7c;
  border: 1px solid #e4e7ed;
  margin-bottom: 4px;
  cursor: pointer;
  color: #f2f6fc;
  padding: 8px 12px;
  border-radius: 4px;
  position: absolute;
  left: 50%;
  bottom: 30px;
  transform: translateX(-50%);
}
</style>